/**
 * mqtt 客户端
 */
const mosca = require('mosca')

const server = new mosca.Server({
  port: 8081
})


// 当有客户端连接时的回调
server.on('clientConnected', (client) => {
  console.log(`已连接的id`)
  // console.log(client)
})

server.on('published', (packet, client) => {
  console.log('===============[ published ]======================')
  // console.log(packet)
  console.log(client)


  // 跟客户端约定 通讯的topic
  switch (packet.topic) {
    case "presence":
      // 处理消息内容 - 消息是二进制
      console.log(packet.payload.toString())
      server.publish({
        // topic 字段是必要字段， 告知客户端， 我的通讯主题是 sayName ， 客户端订阅即可
        topic: "sayName",
        payload: "我是服务端"
      })

      break;
  }
})

server.on('ready', () => {
  console.log(`服务端启动`)
})

